﻿#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>


//分支与循环小游戏
//1.电脑⾃动⽣成1~100的随机数
//2. 玩家猜数字，猜数字的过程中，根据猜测数据的⼤⼩给出⼤了或⼩了的反馈，直到猜对，游戏结束


void menu()
{
	printf("***********************\n");
	printf("******  1. play  ******\n");
	printf("******  0. exit  ******\n");
	printf("***********************\n");
}
 
void game()
{
	//生成随机数
	int r = rand() % 100 + 1;
	int guess = 0;
	int count = 5;
	//5次猜数机会
	while (count)
	{
		printf("你还有%d次机会\n", count);
		printf("输入数字：");
		scanf("%d", &guess);
		if (guess > r)
			printf("猜大了\n");
		else if (guess < r)
			printf("猜小了\n");
		else
		{
			printf("猜对了！\n");
			break;
		}
		count--;
	}
	if (count == 0)
		printf("你猜错了，正确答案是%d\n", r);

}
 
int main()
{
	int input = 0; 
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请输入指令:");
		scanf("%d", &input);
		switch (input)
		{
			case 1:
				printf("开始游戏\n");
				game();
				break;
			case 0:
				printf("退出游戏\n");
				break;
			default:
				printf("指令有误\n");
				break;
		}
	} while (input);
}







